home *** CD-ROM | disk | FTP | other *** search
/ PC PowerPlay 58 / pcpp58a.iso / extras / quake 3 source / Q3A_ToolSource.exe / Main / SurfaceDlg.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-02  |  23.9 KB  |  963 lines

  1. // SurfaceDlg.cpp : implementation file
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "Radiant.h"
  6. #include "SurfaceDlg.h"
  7. #include "PrefsDlg.h"
  8. #include "mainfrm.h"
  9. #include "TextureLayout.h"
  10.  
  11. #ifdef _DEBUG
  12. #define new DEBUG_NEW
  13. #undef THIS_FILE
  14. static char THIS_FILE[] = __FILE__;
  15. #endif
  16.  
  17. /////////////////////////////////////////////////////////////////////////////
  18. // surface properties plugin
  19. // global flag for surface properties plugin is g_qeglobals.bSurfacePropertiesPlugin
  20. _QERPlugSurfaceTable g_SurfaceTable;
  21.  
  22. /////////////////////////////////////////////////////////////////////////////
  23. // CSurfaceDlg dialog
  24.  
  25. CSurfaceDlg g_dlgSurface;
  26.  
  27.  
  28. CSurfaceDlg::CSurfaceDlg(CWnd* pParent /*=NULL*/)
  29.     : CDialog(CSurfaceDlg::IDD, pParent)
  30. {
  31.     //{{AFX_DATA_INIT(CSurfaceDlg)
  32.     m_nHeight = 1;
  33.     m_nWidth = 1;
  34.     //}}AFX_DATA_INIT
  35. }
  36.  
  37.  
  38. void CSurfaceDlg::DoDataExchange(CDataExchange* pDX)
  39. {
  40.     CDialog::DoDataExchange(pDX);
  41.     //{{AFX_DATA_MAP(CSurfaceDlg)
  42.     DDX_Control(pDX, IDC_SPIN_WIDTH, m_wndWidth);
  43.     DDX_Control(pDX, IDC_SPIN_HEIGHT, m_wndHeight);
  44.     DDX_Control(pDX, IDC_SPIN_VSHIFT, m_wndVShift);
  45.     DDX_Control(pDX, IDC_SPIN_VSCALE, m_wndVScale);
  46.     DDX_Control(pDX, IDC_SPIN_ROTATE, m_wndRotate);
  47.     DDX_Control(pDX, IDC_SPIN_HSHIFT, m_wndHShift);
  48.     DDX_Control(pDX, IDC_SPIN_HSCALE, m_wndHScale);
  49.     DDX_Text(pDX, IDC_EDIT_HEIGHT, m_nHeight);
  50.     DDV_MinMaxInt(pDX, m_nHeight, 1, 32);
  51.     DDX_Text(pDX, IDC_EDIT_WIDTH, m_nWidth);
  52.     DDV_MinMaxInt(pDX, m_nWidth, 1, 32);
  53.     //}}AFX_DATA_MAP
  54. }
  55.  
  56.  
  57. BEGIN_MESSAGE_MAP(CSurfaceDlg, CDialog)
  58.     //{{AFX_MSG_MAP(CSurfaceDlg)
  59.     ON_WM_HSCROLL()
  60.     ON_WM_KEYDOWN()
  61.     ON_WM_VSCROLL()
  62.     ON_BN_CLICKED(IDAPPLY, OnApply)
  63.     ON_WM_CLOSE()
  64.     ON_WM_DESTROY()
  65.     ON_BN_CLICKED(ID_BTN_CANCEL, OnBtnCancel)
  66.     ON_BN_CLICKED(IDC_BTN_COLOR, OnBtnColor)
  67.     ON_WM_CTLCOLOR()
  68.     ON_WM_CREATE()
  69.     ON_NOTIFY(UDN_DELTAPOS, IDC_SPIN_HSHIFT, OnDeltaPosSpin)
  70.     ON_BN_CLICKED(IDC_BTN_PATCHDETAILS, OnBtnPatchdetails)
  71.     ON_BN_CLICKED(IDC_BTN_PATCHNATURAL, OnBtnPatchnatural)
  72.     ON_BN_CLICKED(IDC_BTN_PATCHRESET, OnBtnPatchreset)
  73.     ON_BN_CLICKED(IDC_BTN_PATCHFIT, OnBtnPatchfit)
  74.     ON_NOTIFY(UDN_DELTAPOS, IDC_SPIN_HSCALE, OnDeltaPosSpin)
  75.     ON_NOTIFY(UDN_DELTAPOS, IDC_SPIN_ROTATE, OnDeltaPosSpin)
  76.     ON_NOTIFY(UDN_DELTAPOS, IDC_SPIN_VSCALE, OnDeltaPosSpin)
  77.     ON_NOTIFY(UDN_DELTAPOS, IDC_SPIN_VSHIFT, OnDeltaPosSpin)
  78.     ON_BN_CLICKED(IDC_BTN_AXIAL, OnBtnAxial)
  79.     ON_BN_CLICKED(IDC_BTN_BRUSHFIT, OnBtnBrushfit)
  80.     ON_BN_CLICKED(IDC_BTN_FACEFIT, OnBtnFacefit)
  81.     //}}AFX_MSG_MAP
  82. END_MESSAGE_MAP()
  83.  
  84. /////////////////////////////////////////////////////////////////////////////
  85. // CSurfaceDlg message handlers
  86.  
  87.  
  88. /*
  89. ===================================================
  90.  
  91.   SURFACE INSPECTOR
  92.  
  93. ===================================================
  94. */
  95.  
  96. texdef_t    g_old_texdef;
  97. texdef_t    g_patch_texdef;
  98. HWND        g_surfwin = NULL;
  99. qboolean    g_changed_surface;
  100.  
  101. int    g_checkboxes[64] = { 
  102.     IDC_CHECK1, IDC_CHECK2, IDC_CHECK3, IDC_CHECK4, 
  103.     IDC_CHECK5, IDC_CHECK6, IDC_CHECK7, IDC_CHECK8, 
  104.     IDC_CHECK9, IDC_CHECK10, IDC_CHECK11, IDC_CHECK12, 
  105.     IDC_CHECK13, IDC_CHECK14, IDC_CHECK15, IDC_CHECK16,
  106.     IDC_CHECK17, IDC_CHECK18, IDC_CHECK19, IDC_CHECK20,
  107.     IDC_CHECK21, IDC_CHECK22, IDC_CHECK23, IDC_CHECK24,
  108.     IDC_CHECK25, IDC_CHECK26, IDC_CHECK27, IDC_CHECK28,
  109.     IDC_CHECK29, IDC_CHECK30, IDC_CHECK31, IDC_CHECK32,
  110.  
  111.     IDC_CHECK33, IDC_CHECK34, IDC_CHECK35, IDC_CHECK36,
  112.     IDC_CHECK37, IDC_CHECK38, IDC_CHECK39, IDC_CHECK40,
  113.     IDC_CHECK41, IDC_CHECK42, IDC_CHECK43, IDC_CHECK44,
  114.     IDC_CHECK45, IDC_CHECK46, IDC_CHECK47, IDC_CHECK48,
  115.     IDC_CHECK49, IDC_CHECK50, IDC_CHECK51, IDC_CHECK52,
  116.     IDC_CHECK53, IDC_CHECK54, IDC_CHECK55, IDC_CHECK56,
  117.     IDC_CHECK57, IDC_CHECK58, IDC_CHECK59, IDC_CHECK60,
  118.     IDC_CHECK61, IDC_CHECK62, IDC_CHECK63, IDC_CHECK64
  119.  };
  120.  
  121. /*
  122. ==============
  123. SetTexMods
  124.  
  125. Set the fields to the current texdef
  126. if one face selected -> will read this face texdef, else current texdef
  127. if only patches selected, will read the patch texdef
  128. ===============
  129. */
  130.  
  131. bool g_bNewFace = false;
  132. bool g_bNewApplyHandling = false;
  133. bool g_bGatewayhack = false;
  134.  
  135. void CSurfaceDlg::SetTexMods()
  136. {
  137.     char    sz[128];
  138.     texdef_t *pt;
  139.     brushprimit_texdef_t    *bpt;
  140.     // local copy if a width=2 height=2 qtetxture_t is needed
  141.     brushprimit_texdef_t    local_bp;
  142.     int        i;
  143.  
  144.     if (!g_surfwin)
  145.         return;
  146.  
  147.     m_bPatchMode = false;
  148.  
  149.     if (OnlyPatchesSelected())
  150.     {
  151.         pt = &g_qeglobals.d_texturewin.texdef;
  152.         if (QE_SingleBrush())
  153.     {
  154.             //strcpy(g_patch_texdef.name, Patch_GetTextureName());
  155.             g_patch_texdef.SetName(Patch_GetTextureName());
  156.     }
  157.         else
  158.     {
  159.             //strcpy(g_patch_texdef.name, pt->name);
  160.             g_patch_texdef.SetName(pt->name);
  161.     }
  162.         g_patch_texdef.contents = pt->contents;
  163.         g_patch_texdef.flags = pt->flags;
  164.         g_patch_texdef.value = pt->value;
  165.         pt = &g_patch_texdef;
  166.         m_bPatchMode = true;
  167.     }
  168.     else
  169.     {
  170.         if (g_bNewFace && g_ptrSelectedFaces.GetSize() > 0)
  171.         {
  172.       face_t *selFace = reinterpret_cast<face_t*>(g_ptrSelectedFaces.GetAt(0));
  173.             pt = &selFace->texdef;
  174.             if (g_qeglobals.m_bBrushPrimitMode)
  175.             {
  176.                 // compute a texture matrix related to the default matrix width=2 height=2
  177.                 ConvertTexMatWithQTexture( &selFace->brushprimit_texdef, selFace->d_texture, &local_bp, NULL );
  178.                 bpt = &local_bp;
  179.             }
  180.         }
  181.         else
  182.         {
  183.             pt = &g_qeglobals.d_texturewin.texdef;
  184.             if (g_qeglobals.m_bBrushPrimitMode)
  185.             {
  186.                 bpt = &g_qeglobals.d_texturewin.brushprimit_texdef;
  187.             }
  188.         }
  189.         // brush primitive mode : compute fake shift scale rot representation
  190.         if (g_qeglobals.m_bBrushPrimitMode)
  191.             TexMatToFakeTexCoords( bpt->coords, m_shift, &m_rotate, m_scale );
  192.     }
  193.  
  194.     SendMessage (WM_SETREDRAW, 0, 0);
  195.  
  196.     ::SetWindowText(GetDlgItem(IDC_TEXTURE)->GetSafeHwnd(), pt->name);
  197.  
  198.     if (m_bPatchMode)
  199.         sprintf(sz, "%4.6f", pt->shift[0]);
  200.     else
  201.         if (g_qeglobals.m_bBrushPrimitMode)
  202.             sprintf(sz, "%d", (int)m_shift[0]);
  203.         else
  204.             sprintf(sz, "%d", (int)pt->shift[0]);
  205.     ::SetWindowText(GetDlgItem(IDC_HSHIFT)->GetSafeHwnd(), sz);
  206.  
  207.     if (m_bPatchMode)
  208.         sprintf(sz, "%4.6f", pt->shift[1]);
  209.     else
  210.         if (g_qeglobals.m_bBrushPrimitMode)
  211.             sprintf(sz, "%d", (int)m_shift[1]);
  212.         else
  213.             sprintf(sz, "%d", (int)pt->shift[1]);
  214.     ::SetWindowText(GetDlgItem(IDC_VSHIFT)->GetSafeHwnd(), sz);
  215.  
  216.     sprintf(sz, m_bPatchMode ? "%4.6f" : "%4.6f", g_qeglobals.m_bBrushPrimitMode ? m_scale[0] : pt->scale[0]);
  217.     ::SetWindowText(GetDlgItem(IDC_HSCALE)->GetSafeHwnd(), sz);
  218.  
  219.     sprintf(sz, m_bPatchMode ? "%4.6f" : "%4.6f", g_qeglobals.m_bBrushPrimitMode ? m_scale[1] : pt->scale[1]);
  220.     ::SetWindowText(GetDlgItem(IDC_VSCALE)->GetSafeHwnd(), sz);
  221.  
  222.     //++timo compute BProtate as int ..
  223.     sprintf(sz, "%d", g_qeglobals.m_bBrushPrimitMode ? (int)m_rotate : (int)pt->rotate);
  224.     ::SetWindowText(GetDlgItem(IDC_ROTATE)->GetSafeHwnd(), sz);
  225.  
  226.     sprintf(sz, "%d", (int)pt->value);
  227.     ::SetWindowText(GetDlgItem(IDC_VALUE)->GetSafeHwnd(), sz);
  228.  
  229.     for (i=0 ; i<32 ; i++)
  230.         ::SendMessage(GetDlgItem(g_checkboxes[i])->GetSafeHwnd(), BM_SETCHECK, !!(pt->flags&(1<<i)), 0 );
  231.     for (i=0 ; i<32 ; i++)
  232.         ::SendMessage(GetDlgItem(g_checkboxes[32+i])->GetSafeHwnd(), BM_SETCHECK, !!(pt->contents&(1<<i)), 0 );
  233.  
  234.     SendMessage (WM_SETREDRAW, 1, 0);
  235.     InvalidateRect (NULL, true);
  236. }
  237.  
  238. void CSurfaceDlg::GrabPatchMods()
  239. {
  240.     char    sz[128];
  241.     int i;
  242.     bool b;
  243.     texdef_t *pt = & g_patch_texdef;
  244.  
  245.   ::GetWindowText (GetDlgItem(IDC_HSHIFT)->GetSafeHwnd(), sz, 127);
  246.     pt->shift[0] = atof(sz);
  247.  
  248.   ::GetWindowText (GetDlgItem(IDC_VSHIFT)->GetSafeHwnd(), sz, 127);
  249.     pt->shift[1] = atof(sz);
  250.  
  251.   ::GetWindowText(GetDlgItem(IDC_HSCALE)->GetSafeHwnd(), sz, 127);
  252.     pt->scale[0] = atof(sz);
  253.  
  254.   ::GetWindowText(GetDlgItem(IDC_VSCALE)->GetSafeHwnd(), sz, 127);
  255.     pt->scale[1] = atof(sz);
  256.  
  257.   ::GetWindowText(GetDlgItem(IDC_ROTATE)->GetSafeHwnd(), sz, 127);
  258.     pt->rotate = atof(sz);
  259.  
  260.   ::GetWindowText(GetDlgItem(IDC_VALUE)->GetSafeHwnd(), sz, 127);
  261.     pt->value = atof(sz);
  262.  
  263.     pt->flags = 0;
  264.     for (i=0 ; i<32 ; i++)
  265.     {
  266.     b = ::SendMessage(GetDlgItem(g_checkboxes[i])->GetSafeHwnd(), BM_GETCHECK, 0, 0);
  267.         if (b != 1 && b != 0)
  268.             continue;
  269.         pt->flags |= b<<i;
  270.     }
  271.  
  272.     pt->contents = 0;
  273.     for (i=0 ; i<32 ; i++)
  274.     {
  275.     b = ::SendMessage(GetDlgItem(g_checkboxes[32+i])->GetSafeHwnd(), BM_GETCHECK, 0, 0);
  276.         if (b != 1 && b != 0)
  277.             continue;
  278.         pt->contents |= b<<i;
  279.     }
  280.  
  281. }
  282.  
  283. /*
  284. ==============
  285. GetTexMods
  286.  
  287. Reads the fields to get the current texdef
  288. in brush primitive mode, grab the fake shift scale rot and compute a new texture matrix
  289. ===============
  290. */
  291. void CSurfaceDlg::GetTexMods()
  292. {
  293.     char    sz[128];
  294.     texdef_t *pt;
  295.     int        b;
  296.     int        i;
  297.  
  298.     m_bPatchMode = false;
  299.  
  300.     if (OnlyPatchesSelected())
  301.     {
  302.         pt = &g_qeglobals.d_texturewin.texdef;
  303.         m_bPatchMode = true;
  304.     }
  305.     else
  306.     {
  307.         if (g_bNewFace && g_ptrSelectedFaces.GetSize() > 0)
  308.     {
  309.       face_t *selFace = reinterpret_cast<face_t*>(g_ptrSelectedFaces.GetAt(0));
  310.             pt = &selFace->texdef;
  311.     }
  312.         else
  313.     {
  314.             pt = &g_qeglobals.d_texturewin.texdef;
  315.     }
  316.     }
  317.  
  318.     ::GetWindowText (GetDlgItem(IDC_TEXTURE)->GetSafeHwnd(), sz, 127);
  319.     //strncpy (pt->name, sz, sizeof(pt->name)-1);
  320.     pt->SetName(sz);
  321.     if (pt->name[0] <= ' ')
  322.     {
  323.         //strcpy (pt->name, "none");
  324.         pt->SetName("none");
  325.         ::SetWindowText(GetDlgItem(IDC_TEXTURE)->GetSafeHwnd(), pt->name);
  326.     }
  327.  
  328.     ::GetWindowText (GetDlgItem(IDC_HSHIFT)->GetSafeHwnd(), sz, 127);
  329.     ( g_qeglobals.m_bBrushPrimitMode ? m_shift[0] : pt->shift[0] ) = atof(sz);
  330.  
  331.     ::GetWindowText (GetDlgItem(IDC_VSHIFT)->GetSafeHwnd(), sz, 127);
  332.     ( g_qeglobals.m_bBrushPrimitMode ? m_shift[1] : pt->shift[1] ) = atof(sz);
  333.  
  334.     ::GetWindowText(GetDlgItem(IDC_HSCALE)->GetSafeHwnd(), sz, 127);
  335.     ( g_qeglobals.m_bBrushPrimitMode ? m_scale[0] : pt->scale[0] ) = atof(sz);
  336.  
  337.     ::GetWindowText(GetDlgItem(IDC_VSCALE)->GetSafeHwnd(), sz, 127);
  338.     ( g_qeglobals.m_bBrushPrimitMode ? m_scale[1] : pt->scale[1] ) = atof(sz);
  339.  
  340.     ::GetWindowText(GetDlgItem(IDC_ROTATE)->GetSafeHwnd(), sz, 127);
  341.     ( g_qeglobals.m_bBrushPrimitMode ? m_rotate : pt->rotate ) = atof(sz);
  342.  
  343.     ::GetWindowText(GetDlgItem(IDC_VALUE)->GetSafeHwnd(), sz, 127);
  344.     pt->value = atof(sz);
  345.  
  346.     pt->flags = 0;
  347.     for (i=0 ; i<32 ; i++)
  348.     {
  349.         b = ::SendMessage(GetDlgItem(g_checkboxes[i])->GetSafeHwnd(), BM_GETCHECK, 0, 0);
  350.         if (b != 1 && b != 0)
  351.             continue;
  352.         pt->flags |= b<<i;
  353.     }
  354.  
  355.     pt->contents = 0;
  356.     for (i=0 ; i<32 ; i++)
  357.     {
  358.         b = ::SendMessage(GetDlgItem(g_checkboxes[32+i])->GetSafeHwnd(), BM_GETCHECK, 0, 0);
  359.         if (b != 1 && b != 0)
  360.             continue;
  361.         pt->contents |= b<<i;
  362.     }
  363.  
  364.     g_changed_surface = true;
  365.  
  366.     // a local copy of the texture matrix, given for a qtexture_t with width=2 height=2
  367.     brushprimit_texdef_t    local_bp;
  368.     brushprimit_texdef_t    *bpt;
  369.     if (g_qeglobals.m_bBrushPrimitMode)
  370.     {
  371.     face_t *selFace = NULL;
  372.         if (g_bNewFace && g_ptrSelectedFaces.GetSize() > 0)
  373.     {
  374.       selFace = reinterpret_cast<face_t*>(g_ptrSelectedFaces.GetAt(0));
  375.             bpt = &selFace->brushprimit_texdef;
  376.     }
  377.         else
  378.     {
  379.             bpt = &g_qeglobals.d_texturewin.brushprimit_texdef;
  380.     }
  381.         // compute texture matrix
  382.         // the matrix returned must be understood as a qtexture_t with width=2 height=2
  383.         FakeTexCoordsToTexMat( m_shift, m_rotate, m_scale, local_bp.coords );
  384.         // copy the texture matrix in the global struct
  385.         // fit the qtexture if we have a face selected, otherwise g_qeglobals.d_texturewin.brushprimit_texdef uses the basic qtexture_t with width=2 height=2
  386.  
  387.         ConvertTexMatWithQTexture( &local_bp, NULL, bpt, ( ( g_bNewFace && selFace ) ? selFace->d_texture : NULL ) );
  388.     }
  389.     Select_SetTexture(pt,&local_bp);
  390.  
  391.   //if (m_bPatchMode)
  392.   //{
  393.   //  Patch_SetTextureInfo(pt);
  394.   //}
  395.  
  396. }
  397.  
  398. /*
  399. =================
  400. UpdateSpinners
  401. =================
  402. */
  403.  
  404. void CSurfaceDlg::UpdateSpinners(bool bUp, int nID)
  405. {
  406.     texdef_t *pt;
  407.   texdef_t td;
  408.  
  409.   if (m_bPatchMode)
  410.   {
  411.     td.rotate = 0.0;
  412.     td.scale[0] = td.scale[1] = 0.0;
  413.     td.shift[0] = td.shift[1] = 0.0;
  414.     GrabPatchMods();
  415.  
  416.     pt = &g_patch_texdef;
  417.     td.contents = pt->contents;
  418.     td.flags = pt->flags;
  419.     td.value = pt->value;
  420.  
  421.     if (nID == IDC_SPIN_ROTATE)
  422.       {
  423.           if (bUp)
  424.               td.rotate = pt->rotate;
  425.           else
  426.               td.rotate = -pt->rotate;
  427.       }
  428.     else if (nID == IDC_SPIN_HSCALE)
  429.       {
  430.           if (bUp)
  431.               td.scale[0] = 1-pt->scale[0];
  432.           else
  433.               td.scale[0] = 1+pt->scale[0];
  434.       }
  435.     else if (nID == IDC_SPIN_VSCALE)
  436.       {
  437.           if (bUp)
  438.               td.scale[1] = 1-pt->scale[1];
  439.           else
  440.               td.scale[1] = 1+pt->scale[1];
  441.       } 
  442.       
  443.     else if (nID == IDC_SPIN_HSHIFT)
  444.       {
  445.           if (bUp)
  446.               td.shift[0] = pt->shift[0];
  447.           else
  448.               td.shift[0] = -pt->shift[0];
  449.       }
  450.     else if (nID == IDC_SPIN_VSHIFT)
  451.       {
  452.           if (bUp)
  453.               td.shift[1] = pt->shift[1];
  454.           else
  455.               td.shift[1] = -pt->shift[1];
  456.       }
  457.     pt = &g_qeglobals.d_texturewin.texdef;
  458.     Patch_SetTextureInfo(&td);
  459.   }
  460.     else
  461.     {
  462.         // in brush primitive mode, will read up-to-date m_shift m_rotate m_scale
  463.         GetTexMods ();
  464.         if (g_bNewFace && g_ptrSelectedFaces.GetSize() > 0)
  465.     {
  466.       face_t *selFace = reinterpret_cast<face_t*>(g_ptrSelectedFaces.GetAt(0));
  467.             pt = &selFace->texdef;
  468.     }
  469.         else
  470.     {
  471.             pt = &g_qeglobals.d_texturewin.texdef;
  472.     }
  473.         if (nID == IDC_SPIN_ROTATE)
  474.         {
  475.             if (g_qeglobals.m_bBrushPrimitMode)
  476.             {
  477.                 if (bUp)
  478.                     m_rotate += 45;
  479.                 else
  480.                     m_rotate -= 45;
  481.             }
  482.             else
  483.             {
  484.                 if (bUp)
  485.                     pt->rotate += 45;
  486.                 else
  487.                     pt->rotate -= 45;
  488.                 if (pt->rotate < 0)
  489.                     pt->rotate += 360;
  490.                 if (pt->rotate >= 360)
  491.                     pt->rotate -= 360;
  492.             }
  493.         }
  494.         else if (nID == IDC_SPIN_HSCALE)
  495.         {
  496.             if (g_qeglobals.m_bBrushPrimitMode)
  497.             {
  498.                 if (bUp)
  499.                     m_scale[0] += 0.1;
  500.                 else
  501.                     m_scale[0] -= 0.1;
  502.             }
  503.             else
  504.             {
  505.                 if (bUp)
  506.                     pt->scale[0] += 0.1;
  507.                 else
  508.                     pt->scale[0] -= 0.1;
  509.             }
  510.         }
  511.         else if (nID == IDC_SPIN_VSCALE)
  512.         {
  513.             if (g_qeglobals.m_bBrushPrimitMode)
  514.             {
  515.                 if (bUp)
  516.                     m_scale[1] += 0.1;
  517.                 else
  518.                     m_scale[1] -= 0.1;
  519.             }
  520.             else
  521.             {
  522.                 if (bUp)
  523.                     pt->scale[1] += 0.1;
  524.                 else
  525.                     pt->scale[1] -= 0.1;
  526.             }
  527.         }
  528.         else if (nID == IDC_SPIN_HSHIFT)
  529.         {
  530.             if (g_qeglobals.m_bBrushPrimitMode)
  531.             {
  532.                 if (bUp)
  533.                     m_shift[0] += 8;
  534.                 else
  535.                     m_shift[0] -= 8;
  536.             }
  537.             else
  538.             {
  539.                 if (bUp)
  540.                     pt->shift[0] += 8;
  541.                 else
  542.                     pt->shift[0] -= 8;
  543.             }
  544.         }
  545.         else if (nID == IDC_SPIN_VSHIFT)
  546.         {
  547.             if (g_qeglobals.m_bBrushPrimitMode)
  548.             {
  549.                 if (bUp)
  550.                     m_shift[1] += 8;
  551.                 else
  552.                     m_shift[1] -= 8;
  553.             }
  554.             else
  555.             {
  556.                 if (bUp)
  557.                     pt->shift[1] += 8;
  558.                 else
  559.                     pt->shift[1] -= 8;
  560.             }
  561.         }
  562.     }
  563.     // a local copy of the texture matrix, given for a qtexture_t with width=2 height=2
  564.     brushprimit_texdef_t    local_bp;
  565.     brushprimit_texdef_t    *bpt;
  566.     if (g_qeglobals.m_bBrushPrimitMode)
  567.     {
  568.     face_t *selFace = NULL;
  569.         if (g_bNewFace && g_ptrSelectedFaces.GetSize() > 0)
  570.     {
  571.       selFace = reinterpret_cast<face_t*>(g_ptrSelectedFaces.GetAt(0));
  572.             bpt = &selFace->brushprimit_texdef;
  573.     }
  574.         else
  575.     {
  576.             bpt = &g_qeglobals.d_texturewin.brushprimit_texdef;
  577.     }
  578.         // compute texture matrix
  579.         // the matrix returned must be understood as a qtexture_t with width=2 height=2
  580.         FakeTexCoordsToTexMat( m_shift, m_rotate, m_scale, local_bp.coords );
  581.         // copy the texture matrix in the global struct
  582.         // fit the qtexture if we have a face selected, otherwise g_qeglobals.d_texturewin.brushprimit_texdef uses the basic qtexture_t with width=2 height=2
  583.         ConvertTexMatWithQTexture( &local_bp, NULL, bpt, ( ( g_bNewFace && selFace ) ? selFace->d_texture : NULL ) );
  584.     }
  585.     // brush primit : will update the widgets after reading back texture matrix and computing fake shift scale rot
  586.     SetTexMods();
  587.     g_changed_surface = true;
  588.     Select_SetTexture(pt,&local_bp);
  589. }
  590.  
  591. void CSurfaceDlg::UpdateSpinners(int nScrollCode, int nPos, CScrollBar* pBar)
  592. {
  593.     texdef_t *pt;
  594.  
  595.     GetTexMods ();
  596.   if (g_bNewFace && g_ptrSelectedFaces.GetSize() > 0)
  597.   {
  598.     face_t *selFace = reinterpret_cast<face_t*>(g_ptrSelectedFaces.GetAt(0));
  599.         pt = &selFace->texdef;
  600.   }
  601.   else
  602.   {
  603.       pt = &g_qeglobals.d_texturewin.texdef;
  604.   }
  605.  
  606.     if ((nScrollCode != SB_LINEUP) && (nScrollCode != SB_LINEDOWN))
  607.         return;
  608.     
  609.   if (pBar->GetSafeHwnd() == ::GetDlgItem(GetSafeHwnd(), IDC_ROTATEA))
  610.     {
  611.         if (nScrollCode == SB_LINEUP)
  612.             pt->rotate += 45;
  613.         else
  614.             pt->rotate -= 45;
  615.  
  616.         if (pt->rotate < 0)
  617.             pt->rotate += 360;
  618.  
  619.         if (pt->rotate >= 360)
  620.             pt->rotate -= 360;
  621.     }
  622.  
  623.   else if (pBar->GetSafeHwnd() == ::GetDlgItem(GetSafeHwnd(), IDC_HSCALEA))
  624.     {
  625.         if (nScrollCode == SB_LINEDOWN)
  626.             pt->scale[0] -= 0.1;
  627.         else
  628.             pt->scale[0] += 0.1;
  629.     }
  630.     
  631.   else if (pBar->GetSafeHwnd() == ::GetDlgItem(GetSafeHwnd(), IDC_VSCALEA))
  632.     {
  633.         if (nScrollCode == SB_LINEUP)
  634.             pt->scale[1] += 0.1;
  635.         else
  636.             pt->scale[1] -= 0.1;
  637.     } 
  638.     
  639.   else if (pBar->GetSafeHwnd() == ::GetDlgItem(GetSafeHwnd(), IDC_HSHIFTA))
  640.     {
  641.         if (nScrollCode == SB_LINEDOWN)
  642.             pt->shift[0] -= 8;
  643.         else
  644.             pt->shift[0] += 8;
  645.     }
  646.     
  647.   else if (pBar->GetSafeHwnd() == ::GetDlgItem(GetSafeHwnd(), IDC_VSHIFTA))
  648.     {
  649.         if (nScrollCode == SB_LINEUP)
  650.             pt->shift[1] += 8;
  651.         else
  652.             pt->shift[1] -= 8;
  653.     }
  654.  
  655.     SetTexMods();
  656.     g_changed_surface = true;
  657.     //++timo if !g_qeglobals.m_bBrushPrimitMode send a NULL brushprimit_texdef
  658.     if (!g_qeglobals.m_bBrushPrimitMode)
  659.     {
  660.         Sys_Printf("Warning : non brush primitive mode call to CSurfaceDlg::GetTexMods broken\n");
  661.         Sys_Printf("          ( Select_SetTexture not called )\n");
  662.     }
  663. //    Select_SetTexture(pt);
  664. }
  665.  
  666. void UpdateSurfaceDialog()
  667. {
  668.     if (g_qeglobals.bSurfacePropertiesPlugin)
  669.     {
  670.         g_SurfaceTable.m_pfnUpdateSurfaceDialog();
  671.     }
  672.     else
  673.     {
  674.         if (g_surfwin)
  675.             g_dlgSurface.SetTexMods();
  676.     }
  677.     g_pParentWnd->UpdateTextureBar();
  678. }
  679.  
  680. bool ByeByeSurfaceDialog();
  681.  
  682. void DoSurface (void)
  683. {
  684.     // surface properties plugin ?
  685.     if (g_qeglobals.bSurfacePropertiesPlugin)
  686.     {
  687.         g_SurfaceTable.m_pfnDoSurface();
  688.         return;
  689.     }
  690.  
  691.   g_bNewFace = g_PrefsDlg.m_bFace;
  692.   g_bNewApplyHandling = g_PrefsDlg.m_bNewApplyHandling;
  693.   g_bGatewayhack = g_PrefsDlg.m_bGatewayHack;
  694.     // save current state for cancel
  695.     g_old_texdef = g_qeglobals.d_texturewin.texdef;
  696.     g_changed_surface = false;
  697.  
  698.   if (g_surfwin == NULL && g_dlgSurface.GetSafeHwnd() == NULL)
  699.   {
  700.     g_patch_texdef.scale[0] = 0.05;
  701.     g_patch_texdef.scale[1] = 0.05;
  702.     g_patch_texdef.shift[0] = 0.05;
  703.     g_patch_texdef.shift[1] = 0.05;
  704.     // use rotation increment from preferences
  705.     g_patch_texdef.rotate = g_PrefsDlg.m_nRotation;
  706.  
  707.     g_dlgSurface.Create(IDD_SURFACE);
  708.     CRect rct;
  709.       LONG lSize = sizeof(rct);
  710.       if (LoadRegistryInfo("Radiant::SurfaceWindow", &rct, &lSize))
  711.       g_dlgSurface.SetWindowPos(NULL, rct.left, rct.top, 0,0, SWP_NOSIZE | SWP_SHOWWINDOW);
  712.  
  713.     Sys_UpdateWindows(W_ALL);
  714.   }
  715.   else
  716.   {
  717.     g_surfwin = g_dlgSurface.GetSafeHwnd();
  718.       g_dlgSurface.SetTexMods ();
  719.     g_dlgSurface.ShowWindow(SW_SHOW);
  720.   }
  721. }        
  722.  
  723. bool ByeByeSurfaceDialog()
  724. {
  725.     // surface properties plugin ?
  726.     if (g_qeglobals.bSurfacePropertiesPlugin)
  727.     {
  728.         return g_SurfaceTable.m_pfnByeByeSurfaceDialog();
  729.     }
  730.  
  731.   if (g_surfwin)
  732.   {
  733.     if (g_bGatewayhack)
  734.       PostMessage(g_surfwin, WM_COMMAND, IDAPPLY, 0);
  735.     else
  736.       PostMessage(g_surfwin, WM_COMMAND, IDCANCEL, 0);
  737.     return true;
  738.   }
  739.   else return false;
  740. }
  741.  
  742. BOOL CSurfaceDlg::OnInitDialog() 
  743. {
  744.     CDialog::OnInitDialog();
  745.     
  746.   g_surfwin = GetSafeHwnd();
  747.     SetTexMods ();
  748.  
  749. #ifdef QUAKE3
  750.   GetDlgItem(IDC_CHECK32)->SetWindowText("Curve");
  751.   GetDlgItem(IDC_CHECK64)->SetWindowText("Inverted");
  752. #endif
  753.  
  754.   m_wndHScale.SetRange(0, 1000);
  755.   m_wndVScale.SetRange(0, 1000);
  756.   m_wndHShift.SetRange(0, 1000);
  757.   m_wndVShift.SetRange(0, 1000);
  758.   m_wndRotate.SetRange(0, 1000);
  759.   m_wndWidth.SetRange(1, 32);
  760.   m_wndHeight.SetRange(1, 32);
  761.  
  762.   LPVOID lpv = g_pParentWnd->GetPlugInMgr().GetSurfaceFlags();
  763.   if (lpv != NULL)
  764.   {
  765.     int i = 0;
  766.     char* p = reinterpret_cast<char*>(lpv);
  767.     char* pBuff = new char[strlen(p)+1];
  768.     strcpy(pBuff, p);
  769.     char* pToken = strtok(pBuff, ";\0");
  770.     while (pToken != NULL)
  771.     {
  772.       GetDlgItem(g_checkboxes[i++])->SetWindowText(pToken);
  773.       pToken = strtok(NULL, ";\0");
  774.     }
  775.   }
  776.  
  777.   if (strstr(g_PrefsDlg.m_strWhatGame, "Quake3") != NULL) {
  778.     for (int i=0 ; i < 64 ; i++) {
  779.       ::EnableWindow(GetDlgItem(g_checkboxes[i])->GetSafeHwnd(), FALSE);
  780.     }
  781.  
  782.   }
  783.  
  784.     return TRUE;  // return TRUE unless you set the focus to a control
  785.                   // EXCEPTION: OCX Property Pages should return FALSE
  786. }
  787.  
  788. void CSurfaceDlg::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar) 
  789. {
  790.     CDialog::OnHScroll(nSBCode, nPos, pScrollBar);
  791.     UpdateSpinners(nSBCode, nPos, pScrollBar);
  792.   Sys_UpdateWindows(W_CAMERA);
  793. }
  794.  
  795. void CSurfaceDlg::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags) 
  796. {
  797.     
  798.     CDialog::OnKeyDown(nChar, nRepCnt, nFlags);
  799. }
  800.  
  801. void CSurfaceDlg::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar) 
  802. {
  803.     CDialog::OnVScroll(nSBCode, nPos, pScrollBar);
  804.     UpdateSpinners(nSBCode, nPos, pScrollBar);
  805.     Sys_UpdateWindows(W_CAMERA);
  806. }
  807.  
  808. void CSurfaceDlg::OnApply() 
  809. {
  810.     GetTexMods ();
  811.   Sys_UpdateWindows(W_CAMERA);
  812.   if (g_bNewApplyHandling)
  813.     OnOK();
  814. }
  815.  
  816. void CSurfaceDlg::OnOK() 
  817. {
  818.     GetTexMods();
  819.   g_surfwin = NULL;
  820.     CDialog::OnOK();
  821.   Sys_UpdateWindows(W_ALL);
  822. }
  823.  
  824. void CSurfaceDlg::OnClose() 
  825. {
  826.   g_surfwin = NULL;
  827.     CDialog::OnClose();
  828. }
  829.  
  830. void CSurfaceDlg::OnCancel() 
  831. {
  832.   if (g_bGatewayhack)
  833.     OnOK();
  834.   else
  835.     OnBtnCancel();
  836. }
  837.  
  838. void CSurfaceDlg::OnDestroy() 
  839. {
  840.   if (GetSafeHwnd())
  841.   {
  842.     CRect rct;
  843.     GetWindowRect(rct);
  844.       SaveRegistryInfo("Radiant::SurfaceWindow", &rct, sizeof(rct));
  845.   }
  846.     CDialog::OnDestroy();
  847.   g_surfwin = NULL;
  848.   Sys_UpdateWindows(W_ALL);
  849. }
  850.  
  851. void CSurfaceDlg::OnBtnCancel() 
  852. {
  853.     g_qeglobals.d_texturewin.texdef = g_old_texdef;
  854.     if (g_changed_surface)
  855.     {
  856.         //++timo if !g_qeglobals.m_bBrushPrimitMode send a NULL brushprimit_texdef
  857.         if (!g_qeglobals.m_bBrushPrimitMode)
  858.         {
  859.             Sys_Printf("Warning : non brush primitive mode call to CSurfaceDlg::GetTexMods broken\n");
  860.             Sys_Printf("          ( Select_SetTexture not called )\n");
  861.         }
  862. //        Select_SetTexture(&g_qeglobals.d_texturewin.texdef);
  863.     }
  864.   g_surfwin = NULL;
  865.   DestroyWindow();
  866. }
  867.  
  868. void CSurfaceDlg::OnBtnColor() 
  869. {
  870. }
  871.  
  872. HBRUSH CSurfaceDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) 
  873. {
  874.     HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
  875.     return hbr;
  876. }
  877.  
  878. int CSurfaceDlg::OnCreate(LPCREATESTRUCT lpCreateStruct) 
  879. {
  880.     if (CDialog::OnCreate(lpCreateStruct) == -1)
  881.         return -1;
  882.     
  883.     return 0;
  884. }
  885.  
  886. BOOL CSurfaceDlg::PreCreateWindow(CREATESTRUCT& cs) 
  887. {
  888.     // TODO: Add your specialized code here and/or call the base class
  889.     
  890.     return CDialog::PreCreateWindow(cs);
  891. }
  892.  
  893.  
  894. void CSurfaceDlg::OnDeltaPosSpin(NMHDR* pNMHDR, LRESULT* pResult) 
  895. {
  896.     NM_UPDOWN* pNMUpDown = (NM_UPDOWN*)pNMHDR;
  897.   UpdateSpinners((pNMUpDown->iDelta > 0), pNMUpDown->hdr.idFrom);
  898.     *pResult = 0;
  899. }
  900.  
  901. void CSurfaceDlg::OnBtnPatchdetails() 
  902. {
  903.   Patch_NaturalizeSelected(true);
  904.   Sys_UpdateWindows(W_ALL);
  905. }
  906.  
  907. void CSurfaceDlg::OnBtnPatchnatural() 
  908. {
  909.   Patch_NaturalizeSelected();
  910.   Sys_UpdateWindows(W_ALL);
  911. }
  912.  
  913. void CSurfaceDlg::OnBtnPatchreset() 
  914. {
  915.   CTextureLayout dlg;
  916.   if (dlg.DoModal() == IDOK)
  917.   {
  918.     Patch_ResetTexturing(dlg.m_fX, dlg.m_fY);
  919.   }
  920.   Sys_UpdateWindows(W_ALL);
  921. }
  922.  
  923. void CSurfaceDlg::OnBtnPatchfit() 
  924. {
  925.   Patch_FitTexturing();
  926.   Sys_UpdateWindows(W_ALL);
  927. }
  928.  
  929. void CSurfaceDlg::OnBtnAxial() 
  930. {
  931.   Select_SetTexture (&g_qeglobals.d_texturewin.texdef, &g_qeglobals.d_texturewin.brushprimit_texdef, true);
  932.     g_changed_surface = true;
  933.   SetTexMods();
  934.   Sys_UpdateWindows(W_ALL);
  935. }
  936.  
  937. void CSurfaceDlg::OnBtnBrushfit() 
  938. {
  939.     // TODO: Add your control notification handler code here
  940.     
  941. }
  942.  
  943. void CSurfaceDlg::OnBtnFacefit() 
  944. {
  945.   UpdateData(TRUE);
  946.   if (g_ptrSelectedFaces.GetSize() == 0)
  947.   {
  948.     brush_t *b;
  949.         for (b=selected_brushes.next ; b != &selected_brushes ; b=b->next)
  950.     {
  951.       for (face_t* pFace = b->brush_faces; pFace; pFace = pFace->next)
  952.       {
  953.         g_ptrSelectedFaces.Add(pFace);
  954.         g_ptrSelectedFaceBrushes.Add(b);
  955.       }
  956.     }
  957.   }
  958.   Select_FitTexture(m_nHeight, m_nWidth);
  959.   SetTexMods();
  960.     g_changed_surface = true;
  961.   Sys_UpdateWindows(W_ALL);
  962. }
  963.